﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Jinjia.Approve.Entity.Entity.JJFlow;
using Jinjia.Approve.IRepository.JJFlow;
using Jinjia.Approve.IService.JJFlow;
using Jinjia.Approve.Model.Input.JJFlow;
using Jinjia.Approve.Model.VM;
using Jinjia.Core.Attribute;
using Jinjia.Core.BaseInfrastructure.BaseModel.BaseModel;
using Jinjia.Core.BaseInfrastructure.StorageModel.Model;
using Microsoft.AspNetCore.Http;

namespace Jinjia.Approve.Service.JJFlow
{
    public class JJFlowMappingService : IJJFlowMappingService
    {
        private readonly IFlowMappingRepository _flowMappingRepository;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public JJFlowMappingService(IFlowMappingRepository flowMappingRepository, IHttpContextAccessor httpContextAccessor)
        {
            _flowMappingRepository = flowMappingRepository;
            _httpContextAccessor = httpContextAccessor;
        }

        public async Task<PageListSearchResultVM<JJMappingViewModel>> GetMappingPageList(JJMappingSearchCondition searchCondition)
        {
            return await _flowMappingRepository.GetMappingPageList(searchCondition, GetUserId());
        }

        [UseTran]
        public virtual async Task<ResponseResult<string>> SaveMapping(JJMappingEditModel model)
        {
            var mappingCount = await _flowMappingRepository.GetMappingCount(model.modular_key, model.id);
            if (mappingCount > 0)
            {
                return new ResponseResult<string>()
                {
                    IsSuccess = false,
                    Message = "业务类型code重复"
                };
            }

            var mapping = new jj_flow_mapping_tbl
            {
                id = model.id
            };
            if (mapping.id == 0)
            {
                mapping.system_key = "oak";
                mapping.big_modular_key = string.Empty;
                mapping.deleted = 0;
                mapping.create_user_id = mapping.update_user_id = GetUserId();
                mapping.create_time = mapping.update_time = DateTime.Now;
            }
            else
            {
                mapping = await _flowMappingRepository.GetMappingId(mapping.id);
                if (mapping == null)
                {
                    return new ResponseResult<string>()
                    {
                        IsSuccess = false,
                        Message = "业务类型id不存在"
                    };
                }
            }

            mapping.modular_name = model.modular_name;
            mapping.modular_key = model.modular_key;
            mapping.remark = model.remark;
            mapping.update_user_id = GetUserId();
            mapping.todo_list_type = model.todo_list_type;
            await _flowMappingRepository.Save(mapping);
            return new ResponseResult<string>()
            {
                IsSuccess = true,
            };
        }

        private int GetUserId()
        {
            var currentUserId = GetClaim("id");
            var result = int.TryParse(currentUserId, out int userId);
            return result ? userId : -100;
        }

        /// <summary>
        /// 获取授权用户身份信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private string GetClaim(string type)
        {
            return _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(x => x.Type == type)?.Value;
        }
    }
}